package Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entity.Experiment;
import Entity.Room;
import Entity.Selection;
import Service.ExperimentService;
import Service.RoomService;
import Service.SelectionService;

public class CheckExpServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CheckExpServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String mid=request.getParameter("mid");
		String mname=request.getParameter("mname");
		
		
		
		
		//op=detail&Expid=<%=t.getId()%>&ExpTe_num=<%=t.getTe_number()%>&ExpTe_name=<%=t.getTe_name() %>">
		String op=request.getParameter("op");
		if(op!=null&&op!=""&&op.equals("detail")){//查看详情
			String url=request.getParameter("url");
			String id=request.getParameter("Expid");
			int Expid=-1;
			if(id!=null&&id!="") Expid=Integer.parseInt(id);
			SelectionService ser=new SelectionService();
			List<Selection> list=ser.findByEx_id(Expid);
			int num=0;//报名人数
			if(list!=null) num=list.size();
			String ExpTe_num=request.getParameter("ExpTe_num");
			String ExpTe_name=request.getParameter("ExpTe_name");
			String name=request.getParameter("name");
			request.setAttribute("list", list);
			request.setAttribute("mid", mid);
			request.setAttribute("mname", mname);
			request.setAttribute("ExpTe_num",ExpTe_num);
			request.setAttribute("ExpTe_name",ExpTe_name);
			request.setAttribute("url", url);
			request.setAttribute("name",name);
			request.setAttribute("num",String.valueOf(num));
			request.getRequestDispatcher("/WEB-INF/page/CheckExpDetail.jsp").forward(request,response);
			return;
			//还要返回机房老师的工作号
			//request.setAttribute("number", number);
		}else if(op!=null&&op!=""&&op.equals("ret")){
			request.setAttribute("mid", mid);
			request.setAttribute("mname", mname);
			request.getRequestDispatcher("/WEB-INF/page/CheckRoom.jsp").forward(request,response);
			return;
		}else if(op!=null&&op!=""&&op.equals("back")){
			//System.out.print("ok");
			request.setAttribute("mid", mid);
			request.setAttribute("mname", mname);
			request.getRequestDispatcher("/WEB-INF/page/CheckExp.jsp").forward(request,response);
			return;
		}
		ExperimentService ExpSer=new ExperimentService();
		SelectionService SSer=new SelectionService();
		List<Experiment> ExpList=ExpSer.findAllExperiment();
		List<Integer> num=new ArrayList<Integer>();
		for(int i=0;i<ExpList.size();i++){
			int n=0;
			int id=ExpList.get(i).getId();
			List<Selection> tmp=SSer.findByEx_id(id);
			if(tmp!=null) n=tmp.size();
			num.add(n);
		}
		//num[i]对应第i个实验ExpList[i]的数量
		request.setAttribute("mid", mid);
		request.setAttribute("mname", mname);
		request.setAttribute("ExpList", ExpList);
		request.setAttribute("num", num);
		request.getRequestDispatcher("/WEB-INF/page/CheckExp.jsp").forward(request,response);
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
